package com.jason.platform.supervision.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.*;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.adapter.DailySignAdapter;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.SignBean;
import com.jason.platform.supervision.beans.SignResp;
import com.jason.platform.supervision.task.QuerySignListTask;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.task.TaskListener;
import com.jason.platform.supervision.utils.SharedPreferencesUtils;
import com.jason.platform.supervision.widght.LoadingDialog;
import com.jason.platform.supervision.widght.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: DailySignActivity
 * @Description: 日常签到
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-25 09:47:20
 */
public class DailySignActivity extends BaseActivity implements View.OnClickListener, TaskListener {

    private static final String TAG = DailySignDetailActivity.class.getSimpleName();

    private TitleBar mTitleBar;

    private MyHandler<DailySignActivity> mHandler;
    private View mFootView;
    private String mName;
    private String mPhone;

    //搜索
    private EditText mNameEditView, mPhoneEditView;
    private Button mSearchButton;

    private ListView mListView;
    private DailySignAdapter mAdapter;

    private List<SignBean> mSignBeans = new ArrayList<SignBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_daily_sign);
        super.onCreate(savedInstanceState);
        mHandler = new MyHandler<DailySignActivity>(this);
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        initSearchView();
        mListView = (ListView) findViewById(R.id.daily_sign_list);
        mAdapter = new DailySignAdapter(this);
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(this, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.GONE);
        mListView.addFooterView(mFootView);
        mListView.setAdapter(mAdapter);

        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    if (view.getLastVisiblePosition() == (view.getCount() - 1)) {//滚动到底部
                        if (!mIsLoading && mHasNextPage) {
                            loadMore();
                        } else {
                            if (mFootView != null) {
                                mFootView.setVisibility(View.GONE);
                            }
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SignBean bean = mSignBeans.get(position);
                Intent intent = new Intent(DailySignActivity.this, DailySignDetailActivity.class);
                intent.putExtra(DailySignDetailActivity.KEY_SIGN, bean);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mIsLoading = true;
        mIsStart = true;
        mCurrentIndex = 0;
        getSearchValue();
        QuerySignListTask task = new QuerySignListTask(this);
        task.type = Constant.Sign.QUERY_SIGN;
        task.mParams.put("currentIndex", String.valueOf(++mCurrentIndex));
        task.mParams.put("organId", SharedPreferencesUtils.getOrganId(this).toString());
        task.mParams.put("name", mName);
        task.mParams.put("telephone", mPhone);
        task.execute();
    }

    @Override
    protected void initLoadingDialog() {
        super.initLoadingDialog();
        mLoadingDialog = new LoadingDialog(this);
        mLoadingDialog.setIsOut(false);
        mLoadingDialog.setMessage("加载中");
    }

    private void initSearchView() {
        mNameEditView = (EditText) findViewById(R.id.search_header_name);
        mPhoneEditView = (EditText) findViewById(R.id.search_header_phone);
        mSearchButton = (Button) findViewById(R.id.search_header_search_btn);
        mSearchButton.setOnClickListener(this);
    }

    private void getSearchValue() {
        mName = mNameEditView.getText().toString();
        mPhone = mPhoneEditView.getText().toString();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.search_header_search_btn) {
            //TODO 调用接口，进行搜索
            mLoadingDialog.show();
            initData();
        }
    }

    private void loadMore() {
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(this, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.VISIBLE);
        mListView.setSelection((mListView.getAdapter().getCount() - 1));
        mIsLoading = true;
        mIsStart = false;
        getSearchValue();
        QuerySignListTask task = new QuerySignListTask(this);
        task.type = Constant.Sign.QUERY_SIGN;
        task.mParams.put("currentIndex", String.valueOf(++mCurrentIndex));
        task.mParams.put("organId", SharedPreferencesUtils.getOrganId(this).toString());
        task.mParams.put("name", mName);
        task.mParams.put("telephone", mPhone);
        task.execute();
    }

    @Override
    public void onTaskResult(Task task) {
        Message msg = new Message();
        msg.obj = task;
        mHandler.sendMessage(msg);
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {
        Task task = (Task) msg.obj;
        if (task.type == Constant.Sign.QUERY_SIGN) {
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
            }
            if (task.isSuccess) {
                SignResp resp = (SignResp) task.resData;
                if (mIsStart) {
                    mSignBeans.clear();
                }
                mSignBeans.addAll(resp.getValues());
                if (mSignBeans != null && !mSignBeans.isEmpty()) {
                    if (mAdapter != null) {
                        mAdapter.addAll(mSignBeans);
                        mAdapter.notifyDataSetChanged();
                    }
                }
                mHasNextPage = !resp.isLastPage();
                mIsLoading = false;
                if (mHasNextPage) {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.GONE);
                    }
                }
            } else {
                if (mAdapter != null) {
                    mAdapter.addAll(new ArrayList<SignBean>());
                    mAdapter.notifyDataSetChanged();
                }
                if (mFootView != null) {
                    mFootView.setVisibility(View.GONE);
                }
                Toast.makeText(this, task.rspDesc, Toast.LENGTH_SHORT).show();
            }
        }
    }
}
